// ignore_for_file: depend_on_referenced_packages

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:encrypt/encrypt.dart' as encrypt;
import 'package:flutter/cupertino.dart';
import 'package:xml/xml.dart';
import 'DoiManager.dart';
import 'ase.dart';
import 'package:dio/dio.dart';

void main() {
  runApp(const MyAppClient());
}

class MyAppClient extends StatefulWidget {
  const MyAppClient({super.key});
  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyAppClient> {
  // This widget is the root of your application.

  late WebViewController controller = WebViewController();
  final List urls = [];
  var requestIndex = 0;

  @override
  void initState() {
    super.initState();
    // getAesData();
    requestURL("http://mobile.sp.airkj.com/");
  }

  void requestURL(String url) {
    if (url.isEmpty) {
      return;
    }
    setState(() {
      controller
        // ..setJavaScriptMode(JavaScriptMode.unrestricted)
        ..loadRequest(Uri.parse(url))
        ..setNavigationDelegate(
            NavigationDelegate(onNavigationRequest: (request) {
          return NavigationDecision.navigate;
        }, onWebResourceError: (WebResourceError error) {
          print("error:::::>>>${error.description}");
          // requestIndex += 1;
          // if (requestIndex < urls.length) {
          //   var url = urls[requestIndex];
          //   requestURL(url);
          // }
        }));
    });
  }

  Future<void> getAesData() async {
    var key = "3dd19414e91ac01b";
    var iv = "3dd19414e91ac01b";

    // var url = AES.encryptAes(
    //     plainText: "https://zxzxzx.ymjdcmi.cn/shengtong/",
    //     keyStr: key,
    //     ivStr: iv);
    // print(url);

    Options options = Options(
      headers: {'Accept': "*/*"},
      // 请求的Content-Type，默认值是"application/jrson; charset=utf-8",Headers.formUrlEncodedContentType会自动编码请求体.
      contentType: Headers.jsonContentType,
      // 表示期望以那种格式(方式)接受响应数据。接受4种类型 `json`, `stream`, `plain`, `bytes`. 默认值是 `json`,
      responseType: ResponseType.plain,
    );
    DioManager().request("https://www.xieganchuangke.com/website.xml",
        method: DioMethod.get,
        params: {},
        options: options, successCallBack: (data) {
      var xmlString = data;
      var xml = XmlDocument.parse(xmlString);
      for (final item in xml.findAllElements('url')) {
        var url = item.innerText;
        var desAesUrl =
            AES.decryptAes(encryptedStr: url, keyStr: key, ivStr: iv);
        urls.add(desAesUrl);
      }
      if (urls.isNotEmpty) {
        var url = urls[requestIndex];
        requestURL(url);
      }
    }, errorCallBack: (error) {});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: '',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          // This is the theme of your application.
          //
          // Try running your application with "flutter run". You'll see the
          // application has a blue toolbar. Then, without quitting the app, try
          // changing the primarySwatch below to Colors.green and then invoke
          // "hot reload" (press "r" in the console where you ran "flutter run",
          // or simply save your changes to "hot reload" in a Flutter IDE).
          // Notice that the counter didn't reset back to zero; the application
          // is not restarted.
          primarySwatch: Colors.blue,
        ),
        home: Scaffold(
            body: Column(
          children: [Expanded(child: WebViewWidget(controller: controller))],
        )));
  }
}
